//
//  XJmapViewController.m
//  BaiduMapkit
//
//  Created by chenxinju on 16/6/25.
//  Copyright © 2016年 Innovation. All rights reserved.
//

#import "XJMapViewController.h"
#import <MapKit/MapKit.h>
#import "MyAnnotationView.h"

#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>

@interface XJMapViewController ()<BMKLocationServiceDelegate,BMKPoiSearchDelegate,BMKMapViewDelegate,BMKGeoCodeSearchDelegate>


/**
 *  定义类似于位置管理器的 负责所有定位功能
 */
@property (nonatomic, strong) BMKLocationService *locationService;

/**
 *  地理编码
 */
@property (nonatomic, strong) BMKGeoCodeSearch *genCoder;

//显示地图的view
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;

@end

@implementation XJMapViewController

//定位
- (IBAction)buttonClick:(id)sender {
    
    //设置地图区域
    [self.mapView setRegion:BMKCoordinateRegionMake(self.locationService.userLocation.location.coordinate, BMKCoordinateSpanMake(0.01, 0.01))animated:YES];
}

//检索
- (IBAction)poiButtonClick:(id)sender {

    BMKPoiSearch *poiSearch = [[BMKPoiSearch alloc]init];
    poiSearch.delegate = self;
    
    //创建POI搜索设置
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    
    option.location = self.locationService.userLocation.location.coordinate;
    option.pageIndex = 1; //显示也数
    option.pageCapacity = 30; //一页显示的条数
    option.keyword = @"餐馆"; //搜索类型
    option.radius = 500.f;   //搜索范围
    //开始搜索
    [poiSearch poiSearchNearBy:option];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置显示的mapView的代理
    self.mapView.delegate = self;
    
    //2. 设置用户位置的代理
    self.locationService.delegate = self;
    
    //3. 开始定位 startUserLocationService 封装了开始定位和授权定位
    [self.locationService startUserLocationService];
    
    
    //4. 设置用户追踪模式
    [self.mapView setUserTrackingMode:BMKUserTrackingModeFollow];
    
    //设置显示地图的位置 默认为NO
    self.mapView.showsUserLocation = YES;
    //更新位置
    self.locationService.distanceFilter = 10;
    
    
    //地理编码
    self.genCoder = [[BMKGeoCodeSearch alloc]init];
    //设置地理编码代码
    self.genCoder.delegate = self;
    BMKGeoCodeSearchOption *option = [[BMKGeoCodeSearchOption alloc]init];
    //城市
    option.city = @"深圳市";
    //街道门地址
    option.address = @"南山区";
    //输出编码信息
    [self.genCoder geoCode:option];
}

#pragma mark -  BMKGeoCodeSearchDelegate编码代理
/**
 *返回地址信息搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结BMKGeoCodeSearch果
 *@param error 错误号，@see BMKSearchErrorCode
 */
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    
    NSLog(@"地址:%@ 经度:%f 纬度:%f",result.address,result.location.longitude,result.location.latitude);
    NSLog(@"%@",result);
    
}


#pragma mark -  BMKLocationServiceDelegate 用户位置定位的代理
/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置 一秒一次更新触发
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    
    NSLog(@"用户位置经度:%f 纬度:%f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude);
    //把用户位置设置给地图
    [self.mapView updateLocationData:userLocation];
    
}

/**
 *定位失败后，会调用此函数
 *@param error 错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error {
    
    NSLog(@"定位失败! 错误码信息为:%@",error);
    
}



#pragma mark -  BMKMapViewDelegate 显示大头针的代理
/**
 *根据anntation生成对应的View
 *@param mapView 地图View
 *@param annotation 指定的标注
 *@return 生成的标注View
 */
//自定义大头针视图
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation {
    
    return [MyAnnotationView  annotationViewWithMapView:mapView andAnntation:annotation];
}

#pragma mark -  BMKPoiSearchDelegate检索位置
/**
 *返回POI搜索结果
 *@param searcher 搜索对象
 *@param poiResult 搜索结果列表
 *@param errorCode 错误号，@see BMKSearchErrorCode
 */
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode {
    
    //遍历输出信息
    for (BMKPoiInfo *info in poiResult.poiInfoList) {
        NSLog(@"名称:%@ 地址:%@ 电话:%@",info.name,info.address,info.phone);
        
        //创建大头针数据模型
        BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];
        annotation.coordinate = info.pt;
        annotation.title = info.name;
        annotation.subtitle = info.address;
        
        //地图添加大头针数据模型
        [self.mapView addAnnotation:annotation];
        
    }
}





#pragma mark -  编码懒加载

- (BMKGeoCodeSearch *)genCoder {
    
    if (_genCoder == nil) {
        
        _genCoder = [[BMKGeoCodeSearch alloc]init];
    }
    return _genCoder;
}



#pragma mark - 获取位置懒加载
- (BMKLocationService *)locationService {
    if (_locationService == nil) {
        
        _locationService = [[BMKLocationService alloc]init];
    }
    
    return  _locationService;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}

@end
